package com.henu.zmb.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.henu.R;
import com.henu.zmb.UpdateGoods_Activity;
import com.henu.zmb.UpdateLoseGoods_Activity;
import com.henu.zmb.entity.Goods;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

/**
 * Created by zhumengbo on 2017/5/16.
 */

public class Goods_adapter extends BaseAdapter{
    ArrayList<Goods> list = new ArrayList<Goods>();
    LayoutInflater inflater;// 布局填充器
    boolean is_go;
    Context co;
    public Goods_adapter(Context context ,boolean is_go) {
        inflater = LayoutInflater.from(context);// 获取当前上下文的布局填充器
        this.is_go = is_go;
        co=context;
    }
    // 添加数据
    public void addAllData(ArrayList<Goods> templist){
        list.clear();
        list.addAll(templist);
    }
    public void addData(ArrayList<Goods> templist){
        list.addAll(templist);
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        // 不需要每次更新都获取控件，只在第一次时进行
        if(convertView == null){
            convertView = inflater.inflate(R.layout.activity_goods_adapter, null);//把 xml文件转化为view视图
            holder = new ViewHolder();
            holder.image = (ImageView)convertView.findViewById(R.id.aga_ivimage);
            holder.title = (TextView)convertView.findViewById(R.id.aga_tvtitle);
            holder.time = (TextView)convertView.findViewById(R.id.aga_tvtime);
            holder.price = (TextView)convertView.findViewById(R.id.aga_tvprice);
            holder.update = (Button)convertView.findViewById(R.id.aga_bupdate);
            convertView.setTag(holder);// 将holder放入converView中
        }else{
            holder = (ViewHolder)convertView.getTag();// 获取之前存放的holder
        }
        //设置当前控件数据（position）
        final  Goods g = list.get(position);
        // 图片显示
        String url = (g.getGoods_Image().getUrl()); // 获取地址
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        if(!is_go){
            holder.update.setVisibility(View.GONE);
        }
        holder.update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(co,UpdateGoods_Activity.class); // 设置跳转的路径
                intent.putExtra("good",g); // 需要实例化Goods
                // 使用上下文对象来运行startActivity ，co 为上下文对象
                co.startActivity(intent); // 跳转
            }
        });
        ImageLoader.getInstance().displayImage(url, holder.image, options);
        holder.title.setText(g.getGoods_Name());
        holder.time.setText(g.getCreatedAt()); // 发布时间
        holder.price.setText("价格:"+ g.getGoods_Price());
        return convertView;
    }

    class ViewHolder {
        ImageView image;
        TextView title;
        TextView price;
        TextView time;
        Button update;
    }

}
